package util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Initializer {

	public static void main(String[] args) {
		try {
			LoadNativeLibs();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static void LoadNativeLibs() throws IOException{
		CopyLibs();
		System.loadLibrary("lpsolve55");
		System.loadLibrary("lpsolve55j");
	}
	/**
	 * Extracts native dlls from jars and copies to current dir
	 * @throws IOException
	 */
	private static void CopyLibs() throws IOException{
		File eins = new File("lpsolve55.dll");
		File zwei = new File("lpsolve55j.dll");
		
		InputStream eis = Thread.currentThread().getContextClassLoader().getResourceAsStream("lpsolve55.dll");
		
		FileOutputStream fos = new FileOutputStream(eins);
		byte[] x = new byte[8192];
		int read = 0;
		while ( (read = eis.read(x)) > 0)
		{
			fos.write(x, 0, read);
		}
		fos.close();
		eis.close();	
		
		eis = Thread.currentThread().getContextClassLoader().getResourceAsStream("lpsolve55j.dll");
		fos = new FileOutputStream(zwei);
		x = new byte[8192];
		read = 0;
		while ( (read = eis.read(x)) > 0)
		{
			fos.write(x, 0, read);
		}
		fos.close();
		eis.close();
	}
}
